package cn.b.sky.mes.pp.controller;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import cn.b.sky.mes.pp.model.condition.OperationTaskConfirmCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.b.sky.common.file.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.mes.pp.enums.OperationTaskStatus;
import cn.b.sky.mes.pp.model.OperationTaskConfirm;
import cn.b.sky.mes.pp.model.OrderType;
import cn.b.sky.mes.pp.service.OperationTaskConfirmService;
import cn.b.sky.mes.pp.service.OrderTypeService;

/**
 * 
 * <strong>Title : ConfirmRecordSelectController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.pp.controller<br>
 * </strong> <strong>Description : </strong>报工记录查询<br>
 * <strong>Create on : 2016-8-10 下午12:59:26<br>
 * </strong>
 * <p>
 * 
 * @author ZhiPeng.Tong<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/OperationTaskConfirm/*")
@Slf4j
public class ConfirmRecordSelectController extends SecurityController {

	@Resource
	ECodeService eCodeService;

	@Resource
	OrderTypeService orderTypeService;

	@Resource
	OperationTaskConfirmService operationTaskConfirmService;

	@RequestMapping("ConfirmRecordSelect_list")
	public String ConfirmRecordSelect_list(Model model, String navTabId,
			@ModelAttribute("condition") OperationTaskConfirmCondition condition) {
		condition.setSiteCode(this.getSiteCode());
		if (StringUtils.isEmpty(condition.getOrderField())) {
			condition.setOrderField("CreateDateTime");
		}
		List<ECode> ecode = eCodeService.selectECodeList(
				ECodeEnums.OPERATIONTASK_STATUS.getCode(),
				SecurityContextUtil.getLanguageCode());
		List<OrderType> orderTypes = orderTypeService.getAllOrderTypes(this
				.getSiteCode());
		List<OperationTaskConfirm> list = operationTaskConfirmService
				.getConfirmRecordSelectByCondition(condition);
		int count = operationTaskConfirmService
				.getConfirmRecordSelectCountByCondition(condition);
		condition.setData(list);
		condition.setTotalCount(count);
		model.addAttribute("orderTypes", orderTypes);
		model.addAttribute("operationTaskStatus", ecode);
		model.addAttribute("page", condition);
		model.addAttribute(NAVTABID, navTabId);
		return "pp/ConfirmRecordSelect/ConfirmRecordSelect_list";
	}

	@RequestMapping("exportConfirmRecordSelectToExcel")
	public void exportConfirmRecordSelectToExcel(
			HttpServletResponse response,
			@ModelAttribute("condition") OperationTaskConfirmCondition condition,
			String navTabId) {
		condition.setSiteCode(getSiteCode());
		if (StringUtils.isEmpty(condition.getOrderField())) {
			condition.setOrderField("CreateDateTime");
		}
		List<OperationTaskConfirm> list = operationTaskConfirmService
				.selectAllConfirmRecordByCondition(condition);
		Map<String, String> map = new LinkedHashMap<String, String>();
		String languageCode = SecurityContextUtil.getLanguageCode();
		map.put("confirmTypeECode",
				getImeViewMessageI18n(languageCode, "confirmType.C"));
		map.put("confirmDateTime",
				getImeViewMessageI18n(languageCode, "confirmTime.C"));
		map.put("startDateTime",
				getImeViewMessageI18n(languageCode, "startTime.C"));
		map.put("completedQuantity",
				getImeViewMessageI18n(languageCode, "completedQuantity.C"));
		map.put("workUnitCode",
				getImeViewMessageI18n(languageCode, "workUnit.C"));
		map.put("personnelCode",
				getImeViewMessageI18n(languageCode, "lastPersonnelCode.C"));
		map.put("operationTaskNum",
				getImeViewMessageI18n(languageCode, "operationTaskNum.C"));
		map.put("statusText",
				getImeViewMessageI18n(languageCode, "operationTaskstatus.C"));
		map.put("productionControlNum",
				getImeViewMessageI18n(languageCode, "productionControlNum.C"));
		map.put("productionOrderNum",
				getImeViewMessageI18n(languageCode, "productionOrderNum.C"));
		map.put("materialCode",
				getImeViewMessageI18n(languageCode, "materialCode.C"));
		map.put("materialText",
				getImeViewMessageI18n(languageCode, "materialText.C"));
		String ConfirmRecordSelectReport = getImeViewMessageI18n(languageCode,
				"ConfirmRecordSelectReport.C");
		// 循环赋值工序任务单状态信息
		for (OperationTaskConfirm ot : list) {
			if (null != ot.getStatusECode()
					&& OperationTaskStatus.INTO_PRODUCTION.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode, "putIntoProduction.B"));
			} else if (null != ot.getStatusECode()
					&& OperationTaskStatus.CREATE.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"create.B"));
			}else if (null != ot.getStatusECode()
					&& OperationTaskStatus.PLACE_ORDER.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"issued.B"));
			}else if (null != ot.getStatusECode()
					&& OperationTaskStatus.SUBMIT.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"personnelConfirm.B"));
			}else if (null != ot.getStatusECode()
					&& OperationTaskStatus.PAUSE.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"suspend.B"));
			}else if (null != ot.getStatusECode()
					&& OperationTaskStatus.COMPLETE.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"completed.B"));
			} else if (null != ot.getStatusECode()
					&& OperationTaskStatus.CLOSE.getValue().equals(ot.getStatusECode())) {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"forceClose.B"));
			}			else {
				ot.setStatusText(getImeViewMessageI18n(languageCode,
						"forceClose.B"));
			}
		}
		try {
			ExcelUtil.exportToExcel(response, ConfirmRecordSelectReport, map,
					list);
		} catch (IOException e) {
			log.error("{}", e);
		}
	}
}
